 <!-- 角色管理 -->
<template>
  <div class="box_container">
    <!-- 输入框 -->
    <div class="box_input">
      角色名称：
      <el-input v-model="role.name" placeholder="请输入角色名称"></el-input>
      描述信息：
      <el-input v-model="role.desc" placeholder="请输入内容"></el-input>
      <el-button type="primary" @click="addRole">添加</el-button>
    </div>
    <!-- 角色列表数据 -->
    <div class="box_table">
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column prop="name" label="角色名称">
        </el-table-column>
        <el-table-column prop="desc" label="说明">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" @click="handleEdit(scope.row)">修改</el-button>
            <el-button size="mini" type="primary" @click="accreDit(scope.row)">授权</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 修改角色弹框 -->
    <el-dialog title="修改信息" :visible.sync="dialogTableVisible">
      <el-form :model="from">
        <el-form-item label="角色名称" :label-width="formLabelWidth">
          <el-input v-model="from.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="描述信息" :label-width="formLabelWidth">
          <el-input v-model="from.desc" autocomplete="off"></el-input>
        </el-form-item>
        <div class="dialog-footer" style="display: flex;justify-content: end;">
          <el-button type="primary" @click="editRole">修改</el-button>
          <el-button @click="dialogTableVisible = false">关闭</el-button>
        </div>
      </el-form>
    </el-dialog>
    <!-- 分页 -->
    <div class="box_paging">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="role.page" :page-sizes="[10, 30, 50, 100]" :page-size="role.limit" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>
    <!-- 回到顶部-->
    <el-backtop target=".el-main" :bottom="20" :right="200" ref="roll"></el-backtop>
    <!-- 树形控件--权限配置 -->
    <el-dialog title="权限配置" :visible.sync="dialogVisible">
      <el-tree :data="initdata" ref="tree" :default-checked-keys="Checkbox" :props="defaultProps" accordion show-checkbox node-key="id" highlight-current>
        <template slot-scope="{ node, data }">
          <span class="custom-tree-node">
            <span>{{ node.label }}</span>
            <span v-if="data.id!=node.label"> {{data.id}}</span>
          </span>
        </template>
      </el-tree>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addMake">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import Apiadmin from "../../api/admin"
export default {
  data() {
    return {
      formLabelWidth: '120px',
      tableData: [], //角色列表数据
      role: {  //添加数据
        name: '',  //搜索名称
        desc: '',
        status: 1,
        limit: 10, //每页显示的数量
        page: 1,  //当前显示的页码
      },
      total: 0, //数据总条数
      from: {  //修改数据
        name: '',  //搜索名称
        desc: '',
        status: 1,
        id: '',
      },
      dialogTableVisible: false, //修改弹出框显示或者隐藏
      dialogVisible: false, //权限配置弹出框显示或者隐藏

      roleSetPermissionDTO: {  //角色授权参数
        roleId: '',
        permissions: []
      },
      initdata: [], //渲染权限列表数据
      Checkbox: null,  //默认勾选

      defaultProps: {  // 配置选项
        children: 'children',
        label: 'label',
      },
    }
  },
  created() {
    this.RoleList()  //角色列表
  },
  computed: {
    pages() {
      return Math.ceil(this.total / this.pageinfo.limit)
    }
  },
  methods: {
    //请求角色列表
    RoleList() {
      Apiadmin.roleList(this.role).then(res => {
        // console.log(res)
        if (res.errno == 200) {
          this.tableData = res.data.items
          this.total = res.data.count  //分页总数据
        }
      })
    },
    //请求添加角色
    addRole() {
      //非空验证
      if (this.role.name == "") {
        this.$message({
          message: '角色名称不能为空!',
          type: 'error'
        });
        return
      }
      if (this.role.desc == "") {
        this.$message({
          message: '描述信息不能为空!',
          type: 'error'
        });
        return
      }
      Apiadmin.addition(this.role).then(res => {
        console.log(res)
        if (res.errno == 200) {
          this.$message({
            message: '添加成功',
            type: 'success'
          });
          this.role = {}  //清空表单数据
          this.RoleList()
        } else {
          this.$message({
            message: "该角色已经存在",
            type: 'error'
          });
        }
      }).catch(err => {
        console.log(err)
      })
    },
    //请求修改角色
    handleEdit(row) {
      // console.log(row)
      this.dialogTableVisible = true
      this.from.name = row.name
      this.from.desc = row.desc
      this.from.status = row.status
      this.from.id = row.id
    },
    //修改角色确定按钮
    editRole() {
      //非空验证
      if (this.from.name == "") {
        this.$message({
          message: '角色名称不能为空!',
          type: 'error'
        });
        return
      }
      if (this.from.desc == "") {
        this.$message({
          message: '描述信息不能为空!',
          type: 'error'
        });
        return
      }
      Apiadmin.revamp(this.from).then(res => {
        // console.log(res)
        if (res.errno == 200) {
          this.$message({
            message: '修改成功',
            type: 'success'
          });
          this.dialogTableVisible = false
          this.RoleList()
        } else {
          this.$message({
            message: "角色已经存在",
            type: 'error'
          });
        }
      }).catch(err => {
        console.log(err)
      })
    },
    //请求权限列表数据
    accreDit(row) {
      // console.log(row)
      this.dialogVisible = true
      this.roleSetPermissionDTO.roleId = row.id
      Apiadmin.impower({ roleId: row.id }).then(res => {
        // console.log(res)
        if (res.errno == 200) {
          this.initdata = res.data.systemPermissions  //权限列表数据
          this.$nextTick(() => {
            this.Checkbox = res.data.assignedPermissions  //选中的权限
          })
        }
      })
    },
    //角色授权确定按钮
    addMake() {
      this.roleSetPermissionDTO.permissions = this.$refs.tree.getCheckedKeys()
      let perms = JSON.stringify(this.roleSetPermissionDTO)
      Apiadmin.permset(perms).then(res => {
        // console.log(res)
        if (res.errno == 200) {
          this.$message({
            message: '授权成功',
            type: 'success'
          });
          // this.Checkbox = '' //清空选中的数据
          this.dialogVisible = false
          this.RoleList()
        } else {
          this.$message({
            message: "授权失败",
            type: 'error'
          });
        }
      }).catch(err => {
        console.log(err)
      })
    },

    // 分页
    handleSizeChange(val) {
      this.$refs.roll.$el.click?.(".el-main")
      // console.log(`每页 ${val} 条`);
      //在最后一页时，切换没有大小，只发出一次数据请求
      let amount = this.role.limit
      this.role.limit = val
      if (Math.ceil(this.total / amount) == this.role.page) {
        this.role.page = this.pages
      }
      this.RoleList()
    },
    handleCurrentChange(val) {
      this.$refs.roll.$el.click?.(".el-main")
      // console.log(`当前页: ${val}`);
      this.role.page = val;
      this.RoleList()
    },

  }
}
</script>
<style scoped  lang="less">
.box_input {
  .el-input {
    width: 20%;
  }
}
.custom-tree-node {
  width: 100%;
  display: flex;
  justify-content: space-between;
}
</style>